package com.cucn.ingmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cucn.ingmall.product.dao.BrandDao;
import com.cucn.ingmall.product.dao.CategoryDao;
import com.cucn.ingmall.product.entity.BrandEntity;
import com.cucn.ingmall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cucn.common.utils.PageUtils;
import com.cucn.common.utils.Query;

import com.cucn.ingmall.product.dao.CategoryBrandRelationDao;
import com.cucn.ingmall.product.entity.CategoryBrandRelationEntity;
import com.cucn.ingmall.product.service.CategoryBrandRelationService;

/**
 * 品牌分类关联业务类
 *
 * @author kouchaojie
 * @email kou1688@foxmail.com
 * @date 2021-11-17 16:27:04
 */
@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;
    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 新增品牌与分类关联关系
     *
     * @param categoryBrandRelation 前端传来的关联关系:brandId,catelogId
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();

        //根据品牌id和分类id查询出name
        categoryBrandRelation.setBrandName(brandDao.selectById(brandId).getName());
        categoryBrandRelation.setCatelogName(categoryDao.selectById(catelogId).getName());
        this.save(categoryBrandRelation);
    }

    /**
     * 关联更新品牌数据
     *
     * @param brandId 品牌id
     * @param name    品牌名字
     */
    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);
        this.update(categoryBrandRelationEntity,
                new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
    }

    /**
     * 关联更新分类数据
     *
     * @param catId 分类id
     * @param name  分类名字
     */
    @Override
    public void updateCategory(Long catId, String name) {
        baseMapper.updateCategory(catId, name);
    }

    /**
     * 根据品牌id获取分类关联的品牌
     *
     * @param catId 分类id
     * @return 分类关联的品牌集合
     */
    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        //查出指定分类下的关联关系
        List<CategoryBrandRelationEntity> relationEntities = baseMapper.selectList(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId)
        );
        //提取出分类下的品牌id
        List<Long> brandIds = relationEntities.stream().map((item) -> {
            return item.getBrandId();
        }).collect(Collectors.toList());
        //根据品牌id集合查询品牌
        List<BrandEntity> brandEntityList = brandService.listByIds(brandIds);

        return brandEntityList;
    }
}